<?php
/**
 * ExtractMoney(提现申请Controller)
 */
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class ExtractMoneyController extends AdminbaseController
{
    //提现申请model
    protected $money_exp_model;
    public function _initialize()
    {
       parent::_initialize();
       $this->money_exp_model = D("Common/MoneyExp");
    }

    // 申请提现列表
    public function index()
    {
        $where = array();

        $state = I('request.state','');
        parent::dict_select(array(array(
            'code' => 'GIVE_MONENY',
            'name' => 'state',
            'text' => '入职状态',
            'value' => $state
        )
        ));
        if($state){
            $where['moneyexp.state']=$state;
        }
        $name = I('request.name');
        $invite_id = I('request.invite_id');
        if($name){
            $where['invite.name'] = array('like',"%$name%");
        }
        if($invite_id){
            $where['moneyexp.invite_id'] = $invite_id;
            $this->assign('invite_id',$invite_id);
        }
        $count = $this->money_exp_model
            ->join('as moneyexp LEFT JOIN __INVITE__ as invite ON moneyexp.invite_id = invite.uid')
            ->join('LEFT JOIN __BANK_INVITE__ as bankinvite ON moneyexp.bank_invite_id = bankinvite.id')
            ->where($where)
            ->order("moneyexp.created_time DESC")
            ->count();
        $page = $this->page($count, 20);
        $file_arr = array(
            'moneyexp.id',
            'invite.name',
            'moneyexp.invite_id',
            'invite.mobile',
            'moneyexp.money',
            'bankinvite.bank_id',
            'bankinvite.bank_number',
            'moneyexp.state',
            'moneyexp.created_time'
        );
        $list = $this->money_exp_model->field(implode(",",$file_arr))
            ->join('as moneyexp LEFT JOIN __INVITE__ as invite ON moneyexp.invite_id = invite.uid')
            ->join('LEFT JOIN __BANK_INVITE__ as bankinvite ON moneyexp.bank_invite_id = bankinvite.id')
            ->where($where)
            ->order("moneyexp.created_time DESC")
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();
        $this->assign('list', $list);
        $this->assign("page", $page->show('Admin'));
        $this->display();
    }

    /**
     * 修改付款状态
     */
    public function changer_state()
    {
        $id = I("get.id",0,'intval');
        $state  = I("get.state",0,'intval');
        $name  = I("get.name",'');
        $money = I("get.money",0,'intval');
        if($state==0||$id==0||$money==0||$name==''){
            $this->error("获取参数错误");
        }
        //修改付款状态
        $msg = get_current_admin_name().'确认'.$name.'已付款'.$money;
        $this->money_exp_model->give_time=time();
        $this->money_exp_model->updated_time=time();
        $this->money_exp_model->updated_time=time();
        $this->money_exp_model->user_id=get_current_admin_id();
        $this->money_exp_model->msg=$msg;
        $this->money_exp_model->state=$state;
        if($this->money_exp_model->where('id='.$id)->save()!==false){
            $this->success("操作成功！");
        }else{
            $this->error("操作失败");
        }
    }
}
